﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web.Http.Routing;
using System.Web.Routing;

namespace System.Web.Http
{
    public static　class HttpRouteCollectionExtensions
    {
        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, string[] namespaces)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }
            HttpRouteValueDictionary defaults2 = new HttpRouteValueDictionary(defaults);
            HttpRouteValueDictionary constraints2 = new HttpRouteValueDictionary(constraints);
            var dataTokens = new HttpRouteValueDictionary(constraints);
            IHttpRoute httpRoute = routes.CreateRoute(routeTemplate, defaults2, constraints2, dataTokens, null);
            routes.Add(name, httpRoute);
            if (namespaces != null && namespaces.Length > 0)
            {
                if (httpRoute.DataTokens["Namespaces"] != null)
                {
                    httpRoute.DataTokens["Namespaces"] = namespaces;
                }
            }
            return httpRoute;

            ////if (routes == null)
            ////{
            ////    throw new ArgumentNullException("routes");
            ////}
            //var defaults2 = new HttpRouteValueDictionary(defaults);
            //var constraints2 = new HttpRouteValueDictionary(constraints);
            ////IDictionary<string, object> dataTokens = null;
            //IDictionary<string, object> dataTokens0 = new HttpRouteValueDictionary();
            ////IHttpRoute httpRoute0 = routes.CreateRoute(routeTemplate, defaults2, constraints2, dataTokens, null);
            //IHttpRoute httpRoute1 = routes.CreateRoute(routeTemplate, defaults2, constraints2, dataTokens0, null);
            //dataTokens0.Add("Namespaces", namespaces);
            //routes.Add(name, httpRoute1);
            ////var httpRoute = routes.MapHttpRoute(name, routeTemplate, defaults, constraints);
            ////if (namespaces != null && namespaces.Length > 0)
            ////{
            ////    if (httpRoute.DataTokens["Namespaces"] != null)
            ////    {
            ////        httpRoute.DataTokens["Namespaces"] = namespaces;
            ////    }
            ////}
            //return httpRoute1;
        }
    }
}
